
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>引入文件 · test search plus plugin</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Introduction-to-React-JS.html" />
    
    
    <link rel="prev" href="Automatic-browser-refresh.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="Introduction-to-Webpack.html">
            
                <a href="Introduction-to-Webpack.html">
            
                    
                    介绍 Webpack
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="Getting-started.html">
            
                <a href="Getting-started.html">
            
                    
                    第一步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="Running-a-workflow.html">
            
                <a href="Running-a-workflow.html">
            
                    
                    开始工作流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="Automatic-browser-refresh.html">
            
                <a href="Automatic-browser-refresh.html">
            
                    
                    浏览器自动刷新
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.4" data-path="Requiring-files.html">
            
                <a href="Requiring-files.html">
            
                    
                    引入文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Introduction-to-React-JS.html">
            
                <a href="Introduction-to-React-JS.html">
            
                    
                    React JS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="Configuring-react-js.html">
            
                <a href="Configuring-react-js.html">
            
                    
                    配置 React JS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="Optimizing-rebundling.html">
            
                <a href="Optimizing-rebundling.html">
            
                    
                    优化重合并
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="Type-checking-with-flow.html">
            
                <a href="Type-checking-with-flow.html">
            
                    
                    Flow
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="CSS-Fonts-Images.md">
            
                <span>
            
                    
                    CSS, Fonts and Images
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="Loading-CSS.html">
            
                <a href="Loading-CSS.html">
            
                    
                    加载 CSS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="Automatic-CSS-refresh.html">
            
                <a href="Automatic-CSS-refresh.html">
            
                    
                    自动刷新 CSS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="Loading-LESS-or-SASS.html">
            
                <a href="Loading-LESS-or-SASS.html">
            
                    
                    加载 LESS 和 SASS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Inlining-images.html">
            
                <a href="Inlining-images.html">
            
                    
                    内联 images
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="Inlining-fonts.html">
            
                <a href="Inlining-fonts.html">
            
                    
                    内联 fonts
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="Deployment-strategies.md">
            
                <span>
            
                    
                    部署策略
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="Structuring-configuration.html">
            
                <a href="Structuring-configuration.html">
            
                    
                    发布配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="Single-bundle.html">
            
                <a href="Single-bundle.html">
            
                    
                    合并成单文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="Split-app-and-vendors.html">
            
                <a href="Split-app-and-vendors.html">
            
                    
                    分离应用和第三方
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="Multiple-entry-points.html">
            
                <a href="Multiple-entry-points.html">
            
                    
                    多重入口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="Lazy-loaded-entry-points.html">
            
                <a href="Lazy-loaded-entry-points.html">
            
                    
                    懒加载入口文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="Isomorphic-app.html">
            
                <a href="Isomorphic-app.html">
            
                    
                    同构渲染
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="Wing-It-Like-a-Pro.html">
            
                <a href="Wing-It-Like-a-Pro.html">
            
                    
                    进阶
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="Optimizing-development.html">
            
                <a href="Optimizing-development.html">
            
                    
                    优化开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="Hot-loading-components.html">
            
                <a href="Hot-loading-components.html">
            
                    
                    热加载组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="JavaScript-next.html">
            
                <a href="JavaScript-next.html">
            
                    
                    使用下一代 JavaScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="Optimizing-caching.html">
            
                <a href="Optimizing-caching.html">
            
                    
                    优化缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="Matchers.html">
            
                <a href="Matchers.html">
            
                    
                    匹配器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="Lazy-loading-entry-points.md">
            
                <span>
            
                    
                    懒加载入口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="Creating-a-common-bundle.md">
            
                <span>
            
                    
                    创建一个复用代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="Understanding-chunks.html">
            
                <a href="Understanding-chunks.html">
            
                    
                    理解 Chunks
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="Authoring-libraries.html">
            
                <a href="Authoring-libraries.html">
            
                    
                    创建库
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >引入文件</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x6A21;&#x5757;">&#x6A21;&#x5757;</h2>
<p>Webpack allows you to use different module patterns, but &quot;under the hood&quot; they all work the same way. All of them also works straight out of the box.</p>
<p>Webpack &#x5141;&#x8BB8;&#x4F60;&#x4F7F;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x6A21;&#x5757;&#x7C7B;&#x578B;&#xFF0C;&#x4F46;&#x662F; &#x201C;&#x5E95;&#x5C42;&#x201D;&#x5FC5;&#x987B;&#x4F7F;&#x7528;&#x540C;&#x4E00;&#x79CD;&#x5B9E;&#x73B0;&#x3002;&#x6240;&#x6709;&#x7684;&#x6A21;&#x5757;&#x90FD;&#x80FD;&#x591F;&#x5F00;&#x7BB1;&#x5373;&#x7528;&#x3002;</p>
<h4 id="es6-&#x6A21;&#x5757;">ES6 &#x6A21;&#x5757;</h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> MyModule <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./MyModule.js&apos;</span>;
</code></pre>
<h4 id="commonjs">CommonJS</h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> MyModule = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./MyModule.js&apos;</span>);
</code></pre>
<h4 id="amd">AMD</h4>
<pre><code class="lang-javascript">define([<span class="hljs-string">&apos;./MyModule.js&apos;</span>], <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">MyModule</span>) </span>{

});
</code></pre>
<h2 id="&#x7406;&#x89E3;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;">&#x7406;&#x89E3;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;</h2>
<p>A module is loaded by filepath. Imagine the following tree structure:</p>
<p>&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x4F1A;&#x6309;&#x5B83;&#x7684;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#x6765;&#x52A0;&#x8F7D;&#xFF0C;&#x770B;&#x4E00;&#x4E0B;&#x4E0B;&#x9762;&#x7684;&#x8FD9;&#x4E2A;&#x7ED3;&#x6784;&#xFF1A;</p>
<ul>
<li>/app<ul>
<li>/modules<ul>
<li>MyModule.js</li>
</ul>
</li>
<li>main.js (entry point)</li>
<li>utils.js</li>
</ul>
</li>
</ul>
<p>Lets open up the <em>main.js</em> file and require <em>app/modules/MyModule.js</em> in the two most common module patterns:</p>
<p>&#x6253;&#x5F00; <em>main.js</em> &#x7136;&#x540E;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E0B;&#x9762;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x5F15;&#x5165; <em>app/modules/MyModule.js</em> </p>
<p><em>app/main.js</em></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// ES6</span>
<span class="hljs-keyword">import</span> MyModule <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./modules/MyModule.js&apos;</span>;

<span class="hljs-comment">// CommonJS</span>
<span class="hljs-keyword">var</span> MyModule = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./modules/MyModule.js&apos;</span>);
</code></pre>
<p>The <code>./</code> at the beginning states &quot;relative to the file I am in now&quot;.</p>
<p>&#x6700;&#x5F00;&#x59CB;&#x7684; <code>./</code> &#x662F; &#x201C;&#x76F8;&#x5BF9;&#x5F53;&#x524D;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#x201D; </p>
<p>Now let us open the <em>MyModule.js</em> file and require <strong>app/utils</strong>.</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x6253;&#x5F00; <em>MyModule.js</em> &#x7136;&#x540E;&#x5F15;&#x5165; <strong>app/utils</strong>&#xFF1A;</p>
<p><em>app/modules/MyModule.js</em></p>
<pre><code class="lang-javascript"><span class="hljs-comment">// ES6 &#x76F8;&#x5BF9;&#x8DEF;&#x5F84;</span>
<span class="hljs-keyword">import</span> utils <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./../utils.js&apos;</span>;

<span class="hljs-comment">// ES6 &#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</span>
<span class="hljs-keyword">import</span> utils <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;/utils.js&apos;</span>;

<span class="hljs-comment">// CommonJS &#x76F8;&#x5BF9;&#x8DEF;&#x5F84;</span>
<span class="hljs-keyword">var</span> utils = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./../utils.js&apos;</span>);

<span class="hljs-comment">// CommonJS &#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</span>
<span class="hljs-keyword">var</span> utils = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;/utils.js&apos;</span>);
</code></pre>
<p>The <strong>relative path</strong> is relative to the current file. The <strong>absolute path</strong> is relative to the entry file, which in this case is <em>main.js</em>.</p>
<p><strong>&#x76F8;&#x5BF9;&#x8DEF;&#x5F84;</strong>&#x662F;&#x76F8;&#x5BF9;&#x5F53;&#x524D;&#x76EE;&#x5F55;&#x3002;<strong>&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</strong>&#x662F;&#x76F8;&#x5BF9;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#xFF0C;&#x8FD9;&#x4E2A;&#x6848;&#x4F8B;&#x4E2D;&#x662F; <em>main.js</em>&#x3002;</p>
<h3 id="&#x6211;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x6587;&#x4EF6;&#x540E;&#x7F00;&#x4E48;&#xFF1F;">&#x6211;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x6587;&#x4EF6;&#x540E;&#x7F00;&#x4E48;&#xFF1F;</h3>
<p>No, you do not have to use <em>.js</em>, but it highlights better what you are requiring. You might have some .js files, and some .jsx files and even images and css can be required by Webpack. It also clearly differs from required node_modules and specific files.</p>
<p>&#x4E0D;&#xFF0C;&#x4F60;&#x4E0D;&#x9700;&#x8981;&#x53BB;&#x7279;&#x610F;&#x53BB;&#x4F7F;&#x7528; <em>.js</em>&#xFF0C;&#x4F46;&#x662F;&#x4ED6;&#x80FD;&#x591F;&#x66F4;&#x8BA9;&#x4F60;&#x66F4;&#x6E05;&#x695A;&#x4F60;&#x6B63;&#x5F15;&#x5165;&#x7684;&#x6863;&#x6848;&#x3002;&#x56E0;&#x70BA;&#x4F60;&#x53EF;&#x80FD;&#x6709;&#x4E00;&#x4E9B; .js &#x6587;&#x4EF6;&#x548C;&#x4E00;&#x4E9B; .jsx &#x6587;&#x4EF6;&#xFF0C;&#x751A;&#x81F3;&#x4E00;&#x4E9B;&#x56FE;&#x7247;&#x548C; css &#x53EF;&#x4EE5;&#x7528; Webpack &#x4F86;&#x5F15;&#x5165;&#x3002;&#x52A0;&#x5165;&#x6587;&#x4EF6;&#x540E;&#x7F00;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x6E05;&#x695A;&#x5730;&#x533A;&#x5206;&#x4F60;&#x5F15;&#x5165;&#x7684;&#x662F; node_modules &#x6216;&#x7279;&#x5B9A;&#x6863;&#x6848;&#x8FD8;&#x662F;&#x4E00;&#x822C;&#x6587;&#x4EF6;&#x6863;&#x6848;&#x3002;</p>
<p>Remember that Webpack is a module bundler! This means you can set it up to load any format you want given there is a loader for it. We&apos;ll delve into this topic later on.</p>
<p>&#x8BB0;&#x4F4F;&#xFF0C;Webpack &#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x5408;&#x5E76;&#x5668;&#xFF01;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x4F60;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x4ED6;&#x53BB;&#x52A0;&#x8F7D;&#x4EFB;&#x4F55;&#x4F60;&#x5199;&#x7684;&#x5339;&#x914D;&#xFF0C;&#x53EA;&#x8981;&#x6709;&#x4E00;&#x4E2A;&#x52A0;&#x8F7D;&#x5668;&#x3002;&#x6211;&#x4EEC;&#x7A0D;&#x540E;&#x4F1A;&#x7EE7;&#x7EED;&#x6DF1;&#x5165;&#x8FD9;&#x4E2A;&#x8BDD;&#x9898;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Automatic-browser-refresh.html" class="navigation navigation-prev " aria-label="Previous page: 浏览器自动刷新">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Introduction-to-React-JS.html" class="navigation navigation-next " aria-label="Next page: React JS">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"引入文件","level":"1.2.4","depth":2,"next":{"title":"React JS","level":"1.3","depth":1,"path":"Introduction-to-React-JS.md","ref":"Introduction-to-React-JS.md","articles":[{"title":"配置 React JS","level":"1.3.1","depth":2,"path":"Configuring-react-js.md","ref":"Configuring-react-js.md","articles":[]},{"title":"优化重合并","level":"1.3.2","depth":2,"path":"Optimizing-rebundling.md","ref":"Optimizing-rebundling.md","articles":[]},{"title":"Flow","level":"1.3.3","depth":2,"path":"Type-checking-with-flow.md","ref":"Type-checking-with-flow.md","articles":[]}]},"previous":{"title":"浏览器自动刷新","level":"1.2.3","depth":2,"path":"Automatic-browser-refresh.md","ref":"Automatic-browser-refresh.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro","fontsettings","livereload"],"root":"./content","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"search-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"version":"0.0.0"},"title":"test search plus plugin","gitbook":"*"},"file":{"path":"Requiring-files.md","mtime":"2017-01-24T08:51:37.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-01-24T08:53:13.796Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    

    </body>
</html>

